package com.zhang.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.crm.constant.Constants;
import com.zhang.crm.mapper.TTranmapper;
import com.zhang.crm.pojo.TTran;
import com.zhang.crm.service.RedisService;
import com.zhang.crm.service.TranService;
import com.zhang.crm.util.UserUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TranServiceImpl implements TranService {
    @Resource
    private TTranmapper tTranmapper;
    @Resource
    private RedisService redisService;
    @Override
    @Transactional
    public Boolean addTran(TTran tTran) {
        //创建唯一数
        long l = System.currentTimeMillis();
        //redis
        long redisl = redisService.getUniqueNum();
        tTran.setTranNo(String.valueOf(l) + String.valueOf(redisl));
        tTran.setCreateBy(UserUtil.getUserInfo().getId());
        tTran.setCreateTime(new Date());
        int insert = tTranmapper.insert(tTran);
        return insert > 0 ? true : false;
    }

    @Override
    public PageInfo<TTran> getTranListByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_DATA_SIZE);
        List<TTran> tTranList = tTranmapper.selectTranList();
        PageInfo<TTran> tTranPageInfo = new PageInfo<>(tTranList);
        return tTranPageInfo;
    }

    @Override
    public TTran getTranDetail(Integer tranId) {
        TTran tTran = tTranmapper.selectTranDetail(tranId);
        return tTran;
    }
}
